//#######################################################################################################
//#################################### Plugin-098: External Watchdog ####################################
//#######################################################################################################

/*********************************************************************************************\
 * Simple watchdog plugin for Mega with Ethernet
 * 
 * Author             : Martinus van den Broek
 * Support            : None!
 * Date               : 26 Sep 2013
 * Version            : 1.0
 * Compatibility      : R597
 \*********************************************************************************************/

#define PLUGIN_ID 98
#define PLUGIN_NAME "WatchDog"

boolean Plugin_098(byte function, struct NodoEventStruct *event, char *string)
{
  static boolean watchdog = false;
  boolean success=false;

  switch(function)
  {
#ifdef PLUGIN_098_CORE
  case PLUGIN_COMMAND:
    {
      watchdog = (event->Par1 == VALUE_ON);
      success=true;
      break;
     }

  case PLUGIN_ONCE_A_SECOND:
    {
      if (watchdog)
      {
        if (WireNodo.initCount() > 1)
        {  
          Serial.print("I2C > ");
          Serial.println(WireNodo.initCount());
        }
        if(bitRead(HW_Config,HW_ETHERNET))
          {
            Serial.print("WD! ");
            Serial.println((long)(millis()/1000));
          }
      }
      break;
    }
#endif // PLUGIN_098_CORE

    #if NODO_MEGA
    case PLUGIN_MMI_IN:
      {
      char *TempStr=(char*)malloc(26);
      string[25]=0;
      if(GetArgv(string,TempStr,1))
        {
        if(strcasecmp(TempStr,PLUGIN_NAME)==0)
          {
          if(GetArgv(string,TempStr,2)) 
            {
              event->Type = NODO_TYPE_PLUGIN_COMMAND;
              event->Command = 98;  
              success=true;
            }
          }
        }
      free(TempStr);
      break;
      }

    case PLUGIN_MMI_OUT:
      {
      strcpy(string,PLUGIN_NAME);            // Commando / event 
      strcat(string," ");                
    
      if(event->Par1 == VALUE_ON)
        strcat(string,"On");  
      else
        strcat(string,"Off");  

      break;
      }
    #endif

  }

  return success;
}
